package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.Exception.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.utlis.MD5;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.*;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-26
 */
@RestController//RestController注解中包含了@ResponseBody 他默认使用json工具是jackson
@RequestMapping("/admin/hosp/hospital-set")
@Api(description = "医院设置接口")
//@CrossOrigin 配置gateway网关后,不需要该注解 网关已经解决跨域问题
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;

    @GetMapping("findAll")
    @ApiOperation(value = "医院设置 列表")
    public R findAllHospitalSet(){
        //
        List<HospitalSet> list = hospitalSetService.list(null);
        return R.ok().data("data",list);
    }
    @DeleteMapping("deleteById/{id}")
    @ApiOperation(value = "医院设置 删除")
    public R deleteById(@PathVariable("id")Long id){
        //
        boolean flag = hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation("分页查询 医院设置")
    @GetMapping("findAllPage/{page}/{limit}")
    public R findAllPage(
            @ApiParam(name = "page",value = "当前页码",required = true) @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页条数",required = true)@PathVariable("limit") Long limit){

        //创建分页对象,并传入分页参数
        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);
        //调用分页的方法
        Page<HospitalSet> page1 = hospitalSetService.page(hospitalSetPage);
        //获取分页查询出来的数据
        List<HospitalSet> records = page1.getRecords();//获取查出来的HospitalSet
        long total = page1.getTotal();//获取总记录数

        //统一返回结果
        R r = R.ok().data("HospitalSets", records).data("total", total);
        return r;
    }

    @ApiOperation("分页查询 医院设置")
    @PostMapping("findPageAndCondition/{page}/{limit}")//不可以使用getMapping 原因: get请求没有请求体
    public R findPageAndCondition(//required属性: 是否为必须的
            @ApiParam(name = "page",value = "当前页码",required = true) @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页条数",required = true)@PathVariable("limit") Long limit,
            @ApiParam(name = "hospitalSetQueryVo", value = "查询对象", required = false)
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo  //hospitalSetQueryVo 封装的查询条件:
            ){

        //获取条件封装对象的属性值
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        //创建条件构造  QueryWrapper ： Entity 对象封装操作类
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();

        //判断查询条件是否为空: 注意使用spring里的StringUtils
        if (hosname!=null&&!StringUtils.isEmpty(hosname)) {
            //hosname不为空是执行
            //条件: 医院名称 包含 hoscode
            hospitalSetQueryWrapper.like("hosname", hosname);
        }

        if (hoscode!=null&&!StringUtils.isEmpty(hoscode) ) {
            //hoscode不为空是执行
            //条件: 医院编号等于hoscode
            hospitalSetQueryWrapper.eq("hoscode", hoscode);
        }
        hospitalSetQueryWrapper.orderByDesc("id");//根据id 降序

        //创建分页Page对象,并传入分页参数
        Page<HospitalSet> hospitalSetPage = new Page<>(page,limit);
        //调用 分页和条件 的查询方法方法
        Page<HospitalSet> page1 = hospitalSetService.page(hospitalSetPage,hospitalSetQueryWrapper);
        //获取分页查询出来的数据
        List<HospitalSet> records = page1.getRecords();//获取查出来的HospitalSet
        long total = page1.getTotal();//获取总记录数

        //统一返回结果
        R r = R.ok().data("HospitalSets", records).data("total", total);
        return r;
    }

    @ApiOperation("新增医院设置")
    @PostMapping("add")
    public R add(
            @ApiParam(name = "hospitalSet",value = "医院设置对象",required = true)
            @RequestBody HospitalSet hospitalSet) {
        //设置状态 1 为可用,0为不可用
        hospitalSet.setStatus(1);
        //设置Sing_key(签名秘钥, 随机生成的)
        Random random = new Random();//创建Random(随机)对象
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        //调用添加的方法
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {//添加成功
            return R.ok();
        }else{//添加失败
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable String id){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    @ApiOperation("根据id修改")
    @PostMapping("/updataById")
    public R updataById(
            @ApiParam(name="hospitalSet" ,value = "医院设置对象")
            @RequestBody HospitalSet  hospitalSet){
        hospitalSet.setUpdateTime(null);//将更新时间设置为空,才会自动填充
        //调用根据id修改的方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据ids 批量删除 医院设置")
    @DeleteMapping("deleteByIds")
    public R deleteByIds(
            @ApiParam(name="ids" ,value = "id集合")
            @RequestBody List<Integer> ids){
        //调用根据ID批量删除的方法
        boolean b = hospitalSetService.removeByIds(ids);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("医院设置锁定和解锁")
    @GetMapping("setStatus/{id}/{status}")
    public R setStatus(
         @ApiParam(name="id",value = "医院设置的id")   @PathVariable("id") Integer id,
         @ApiParam(name="status",value = "医院设置的状态status")  @PathVariable("status") Integer status){
      /*  try {//模拟出现了异常
            int i=10/0;
        } catch (Exception e) {
            e.printStackTrace();
            //抛出自定义异常
            throw new YyghException(20001,"自定义异常");
        }*/
        //先根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态 1为锁定 0为可用
        hospitalSet.setStatus(status);
        //调用根据id 更新的方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

